package View;

import Control.ControlOwner;
import Control.ControlPet;
import Control.ControlVeterinarian;
import Services.OwnerService;
import Services.VeterinarianService;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import Entity.Owner;
import Entity.Pet;
import Entity.Vaccine;
import Services.AppointmentService;
import Services.PetService;
import Services.VaccineService;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;

/**
 *
 * @author Nicolas
 */
public class HPVAS extends JFrame {

    private SearchPanel search_panel;
    private JPanel content_panel;
    private JPanel button_panel;
    private JButton create_button;
    private JButton delete_button;
    private JButton update_button;
    private JButton info_button;
    private JPanel current_panel;
    private CreateAdvice adv;

    public void initGUI() {
        setLayout(new BorderLayout());
        setMaximumSize(new Dimension(700, 355));
        setMinimumSize(new Dimension(700, 355));
        setPreferredSize(new Dimension(700, 355));
        setSearch_panel(new SearchPanel());
        setButton_panel(new JPanel());
        setCurrent_panel(new MainPanel());
        setCreate_button(new JButton("Create"));
        setDelete_button(new JButton("Delete"));
        setUpdate_button(new JButton("Update"));
        setInfo_button(new JButton("View info"));
        getButton_panel().setLayout(new FlowLayout());
        getButton_panel().add(getCreate_button());
        getButton_panel().add(getDelete_button());
        getButton_panel().add(getUpdate_button());
        getButton_panel().add(getInfo_button());
        add(getSearch_panel(), BorderLayout.LINE_START);
        setContent_panel(new JPanel());
        getContent_panel().setLayout(new BorderLayout());
        getContent_panel().add(getCurrent_panel(), BorderLayout.CENTER);
        add(getContent_panel(), BorderLayout.CENTER);
        getContent_panel().add(getButton_panel(), BorderLayout.SOUTH);
        setVisible(true);
        setAdv(new CreateAdvice(this));
        //LISTENERS
        getCreate_button().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                create_buttonActionPerformed(evt);
            }
        });

        getDelete_button().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delete_buttonActionPerformed(evt);
            }
        });

        getInfo_button().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                info_buttonActionPerformed(evt);
            }
        });

        getUpdate_button().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                update_buttonActionPerformed(evt);
            }
        });

        this.addWindowListener(new WindowAdapter() {
            /*
             * Manejo del evento EXIT (X)
             */
            @Override
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }

    public HPVAS() {
        initGUI();
        //inicializarBD();
    }

    private void inicializarBD (){
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("HPVAS-2013-1-BPU");
        OwnerService os = new OwnerService(emf);
        PetService ps = new PetService(emf);
        VaccineService vs = new VaccineService(emf);
        AppointmentService as = new AppointmentService (emf);
        
        Date d = new Date(1991,04,28);
        Date d1 = new Date(1988,05,18);
        Date d2 = new Date(1979,12,24);
        Date d3 = new Date(1994,01,04);
        Owner o1 = new Owner("Pablo", "Cifuentes","444-12-5788",d ,"Madriguera No 3");
        Owner o2 = new Owner("Jorge", "Noguera","457-11-9872",d1 , "Madriguera No 2");
        Owner o3 = new Owner("Fabio", "Bolaños","432-14-4504",d2 , "Madriguera No 1");
        Owner o4 = new Owner("Pepe", "Lepu", "785-14-1412",d3 ,"Madriguera No 4");
        os.crear(o1);
        os.crear(o2);
        os.crear(o3);
        os.crear(o4);
        Pet p1 = new Pet("Duffy", "Duck", 2,(float)12.5, "./Pets/duffy.png" ,o1);
        Pet p2 = new Pet("Ductrio", "Gusano", 1,(float)1.5, "./Pets/Ductrio.png" ,o2);
        Pet p3 = new Pet("Goffie", "Cannis", 5,(float)35.2, "./Pets/Goffie.png" ,o2);
        Pet p4 = new Pet("Charmander", "Dragon", 4,(float)100.5, "./Pets/Charmander.png" ,o3);
        Pet p5 = new Pet("Laica", "Cannis", 2,(float)50, "./Pets/Laica.png" ,o4);
        Pet p6 = new Pet("Antonela", "Cannis", 2,(float)45.8, "./Pets/Antonela.png" ,o4);
        ps.crear(p1);
        ps.crear(p2);
        ps.crear(p3);
        ps.crear(p4);
        ps.crear(p5);
        ps.crear(p6);
        
        Vaccine v = new Vaccine("Rabisan", (float)0.01);
        Vaccine v1 = new Vaccine("Derrivac", (float)0.4);
        Vaccine v2 = new Vaccine("Ravdobac", (float)0.1);
        Vaccine v3 = new Vaccine("Antirabic", (float)0.014);
        Vaccine v4 = new Vaccine("Canivac", (float)0.1);
        Vaccine v5 = new Vaccine("Ontavac", (float)1);
        Vaccine v6 = new Vaccine("Trimune", (float)1.8);
        Vaccine v7 = new Vaccine("Alurrabifa", (float)1.2);
        Vaccine v8 = new Vaccine("Rabdomun", (float)2.5);
        Vaccine v9 = new Vaccine("Endurall", (float)0.3);
        Vaccine v10 = new Vaccine("Quetamina", (float)0.05);
        
        vs.crear(v);
        vs.crear(v1);
        vs.crear(v2);
        vs.crear(v3);
        vs.crear(v4);
        vs.crear(v5);
        vs.crear(v6);
        vs.crear(v7);
        vs.crear(v8);
        vs.crear(v9);
        vs.crear(v10);
        
    }   

    private void create_buttonActionPerformed(java.awt.event.ActionEvent evt) {
        getAdv().setVisible(true);
    }

    private void delete_buttonActionPerformed(java.awt.event.ActionEvent evt) {
        if (getCurrent_panel().getClass().toString().equals("class View.MainPanel")) {
            MainPanel panel = (MainPanel) getCurrent_panel();
            if (panel.target != null) {
                switch (panel.target.split("/")[0]) {
                    case "P":
                        ControlPet cp = new ControlPet();
                        cp.removePet(Long.parseLong(panel.target.split("/")[1].toString()));
                        panel.repaintPetTable();
                        break;
                    case "V":
                        ControlVeterinarian vc = new ControlVeterinarian();
                        vc.removeVeterinarian(Long.parseLong(panel.target.split("/")[1].toString()));
                        panel.repaintVeterinarianTable();
                        break;
                    case "O":
                        ControlOwner oc = new ControlOwner();
                        oc.removeOwner(Long.parseLong(panel.target.split("/")[1].toString()));
                        panel.repaintOwnerTable();
                        break;

                }
                panel.target = null;
            }
        }

        if (getCurrent_panel().getClass().toString().equals("class View.PetInfoPanel")) {
            PetInfoPanel panel = (PetInfoPanel) getCurrent_panel();
            ControlPet cp = new ControlPet();
            cp.removePet(panel.getPet().getId());
            current_panel = new MainPanel();
            change_panel();
        }

        if (getCurrent_panel().getClass().toString().equals("class View.OwnerInfoPanel")) {
            OwnerInfoPanel panel = (OwnerInfoPanel) getCurrent_panel();
            ControlOwner co = new ControlOwner();
            co.removeOwner(panel.getOwner().getId());
            current_panel = new MainPanel();
            change_panel();
        }

        if (getCurrent_panel().getClass().toString().equals("class View.VeterinarianInfoPanel")) {
            VeterinarianInfoPanel panel = (VeterinarianInfoPanel) getCurrent_panel();
            ControlVeterinarian cv = new ControlVeterinarian();
            cv.removeVeterinarian(panel.getVeterianarian().getId());
            current_panel = new MainPanel();
            change_panel();
        }
    }

    private void info_buttonActionPerformed(java.awt.event.ActionEvent evt) {
        ControlPet pc = new ControlPet();
        ControlOwner oc = new ControlOwner();
        ControlVeterinarian vc = new ControlVeterinarian();
        System.out.println(info_button.getText().trim());
        switch (info_button.getText().trim()) {
            case "View info":
                if (getCurrent_panel().getClass().toString().equals("class View.MainPanel")) {
                    MainPanel panel = (MainPanel) getCurrent_panel();
                    if (panel.target != null) {
                        switch (panel.target.split("/")[0]) {
                            case "P":
                                current_panel = new PetInfoPanel(pc.readPet(Long.parseLong(panel.target.split("/")[1].toString())), false);
                                change_panel();
                                break;
                            case "V":
                                current_panel = new VeterinarianInfoPanel(vc.readVeterianarian(Long.parseLong(panel.target.split("/")[1].toString())), false);
                                change_panel();
                                break;
                            case "O":
                                current_panel = new OwnerInfoPanel(oc.readOwner(Long.parseLong(panel.target.split("/")[1].toString())), false);
                                change_panel();
                                break;

                        }
                        info_button.setText("Back");

                    }
                }
                break;
            case "Back":
                current_panel = new MainPanel();
                change_panel();
                info_button.setText("View info");
                break;

            case "Add MedRec":
                break;
        }
    }

    private void update_buttonActionPerformed(java.awt.event.ActionEvent evt) {
        ControlPet pc = new ControlPet();
        ControlOwner oc = new ControlOwner();
        ControlVeterinarian vc = new ControlVeterinarian();
        System.out.println(update_button.getText().trim());
        switch (update_button.getText().trim()) {
            case "Update":
                if (getCurrent_panel().getClass().toString().equals("class View.MainPanel")) {
                    MainPanel panel = (MainPanel) getCurrent_panel();
                    if (panel.target != null) {
                        switch (panel.target.split("/")[0]) {
                            case "P":
                                current_panel = new PetInfoPanel(pc.readPet(Long.parseLong(panel.target.split("/")[1].toString())), true);
                                change_panel();
                                info_button.setText("Add MedRec");
                                break;
                            case "V":
                                current_panel = new VeterinarianInfoPanel(vc.readVeterianarian(Long.parseLong(panel.target.split("/")[1].toString())), true);
                                change_panel();
                                info_button.setVisible(false);
                                break;
                            case "O":
                                current_panel = new OwnerInfoPanel(oc.readOwner(Long.parseLong(panel.target.split("/")[1].toString())), true);
                                change_panel();
                                info_button.setText("Add Pet");
                                break;

                        }
                        update_button.setText("Save");

                    }
                }

                if (getCurrent_panel().getClass().toString().equals("class View.PetInfoPanel")) {
                    PetInfoPanel panel = (PetInfoPanel) getCurrent_panel();
                    ControlPet cp = new ControlPet();
                    current_panel = new PetInfoPanel(panel.getPet(), true);
                    change_panel();
                }

                if (getCurrent_panel().getClass().toString().equals("class View.OwnerInfoPanel")) {
                    OwnerInfoPanel panel = (OwnerInfoPanel) getCurrent_panel();
                    ControlOwner co = new ControlOwner();
                    current_panel = new OwnerInfoPanel(panel.getOwner(), true);
                    change_panel();
                }

                if (getCurrent_panel().getClass().toString().equals("class View.VeterinarianInfoPanel")) {
                    VeterinarianInfoPanel panel = (VeterinarianInfoPanel) getCurrent_panel();
                    ControlVeterinarian cv = new ControlVeterinarian();
                    current_panel = new VeterinarianInfoPanel(panel.getVeterianarian(), true);
                    change_panel();
                }


                break;
            case "Save":
                if (getCurrent_panel().getClass().toString().equals("class View.PetInfoPanel")) {
                    try {
                        PetInfoPanel panel = (PetInfoPanel) getCurrent_panel();
                        panel.updatePet();
                        ControlPet cp = new ControlPet();
                        cp.editPet(panel.getPet());
                        current_panel=new PetInfoPanel(panel.getPet(), false);
                        change_panel();
                    } catch (Exception ex) {
                        System.out.println("ERROR AL HACER UPDATE");
                        current_panel=new MainPanel();
                        change_panel();
                    }
                }

                if (getCurrent_panel().getClass().toString().equals("class View.OwnerInfoPanel")) {
                    try {
                        OwnerInfoPanel panel = (OwnerInfoPanel) getCurrent_panel();
                        panel.updateOwner();
                        ControlOwner co = new ControlOwner();
                        co.editOwner(panel.getOwner());
                        current_panel=new OwnerInfoPanel(panel.getOwner(), false);
                        change_panel();
                    } catch (Exception ex) {
                        System.out.println("ERROR AL HACER UPDATE");
                        current_panel=new MainPanel();
                        change_panel();
                    }
                }

                if (getCurrent_panel().getClass().toString().equals("class View.VeterinarianInfoPanel")) {
                   try {
                        VeterinarianInfoPanel panel = (VeterinarianInfoPanel) getCurrent_panel();
                        panel.updateVeterinarian();
                        ControlVeterinarian cv = new ControlVeterinarian();
                        cv.editVeterinarian(panel.getVeterianarian());
                        current_panel=new VeterinarianInfoPanel(panel.getVeterianarian(), false);
                        change_panel();
                    } catch (Exception ex) {
                        System.out.println("ERROR AL HACER UPDATE");
                        current_panel=new MainPanel();
                        change_panel();
                    }
                }
                update_button.setText("Update");
                info_button.setText("View info");
                info_button.setVisible(true);
                        
                break;
        }
    }

    public void change_panel() {
        getContent_panel().removeAll();
        getContent_panel().add(getCurrent_panel(), BorderLayout.CENTER);
        getContent_panel().add(getButton_panel(), BorderLayout.SOUTH);
        getContent_panel().invalidate();
        getContent_panel().validate();
        getContent_panel().repaint();
        this.repaint();
    }

    public static void main(String[] args) {
        HPVAS main = new HPVAS();
    }

    public Dimension getScreen_resolution() {
        return Toolkit.getDefaultToolkit().getScreenSize();
    }

    /**
     * @return the search_panel
     */
    public SearchPanel getSearch_panel() {
        return search_panel;
    }

    /**
     * @param search_panel the search_panel to set
     */
    public void setSearch_panel(SearchPanel search_panel) {
        this.search_panel = search_panel;
    }

    /**
     * @return the content_panel
     */
    public JPanel getContent_panel() {
        return content_panel;
    }

    /**
     * @param content_panel the content_panel to set
     */
    public void setContent_panel(JPanel content_panel) {
        this.content_panel = content_panel;
    }

    /**
     * @return the button_panel
     */
    public JPanel getButton_panel() {
        return button_panel;
    }

    /**
     * @param button_panel the button_panel to set
     */
    public void setButton_panel(JPanel button_panel) {
        this.button_panel = button_panel;
    }

    /**
     * @return the create_button
     */
    public JButton getCreate_button() {
        return create_button;
    }

    /**
     * @param create_button the create_button to set
     */
    public void setCreate_button(JButton create_button) {
        this.create_button = create_button;
    }

    /**
     * @return the delete_button
     */
    public JButton getDelete_button() {
        return delete_button;
    }

    /**
     * @param delete_button the delete_button to set
     */
    public void setDelete_button(JButton delete_button) {
        this.delete_button = delete_button;
    }

    /**
     * @return the update_button
     */
    public JButton getUpdate_button() {
        return update_button;
    }

    /**
     * @param update_button the update_button to set
     */
    public void setUpdate_button(JButton update_button) {
        this.update_button = update_button;
    }

    /**
     * @return the current_panel
     */
    public JPanel getCurrent_panel() {
        return current_panel;
    }

    /**
     * @param current_panel the current_panel to set
     */
    public void setCurrent_panel(JPanel current_panel) {
        this.current_panel = current_panel;
    }

    /**
     * @return the adv
     */
    public CreateAdvice getAdv() {
        return adv;
    }

    /**
     * @param adv the adv to set
     */
    public void setAdv(CreateAdvice adv) {
        this.adv = adv;
    }

    /**
     * @return the os
     *
     * /
     **
     * @return the vs
     */
    /**
     * @return the info_button
     */
    public JButton getInfo_button() {
        return info_button;
    }

    /**
     * @param info_button the info_button to set
     */
    public void setInfo_button(JButton info_button) {
        this.info_button = info_button;
    }
}
